﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using RG.Common.Provider;
using RG.Consts;
using RG.DAO;
using RealtorGuide.DAO;

namespace RealtorGuide.Classes.Impl
{
    public class ApartmentService : IApartmentService 
    {
        [Dependency]
        public IApartmentDAO ApartmentDAO { get; set; }


        [DAOTransaction]
        [RGTransaction]
        public Apartment Get(int id)
        {
            return FromThereToHere<Apartment,Manager,MicroArea,ApartmentDAOObj,ManagerDAOObj,MicroAreaDAOObj>(ApartmentDAO.Get(id), new Apartment());
        }

        [DAOTransaction]
        [RGTransaction]
        public Apartment Put(Apartment item)
        {
            var dao = item.Id > 0 ? ApartmentDAO.Get(item.Id) : new ApartmentDAOObj();

            dao = FromThereToHere<ApartmentDAOObj, ManagerDAOObj, MicroAreaDAOObj,Apartment, Manager, MicroArea>(item, dao);

            if (dao.Id == 0)
                ApartmentDAO.Put(dao);

            return FromThereToHere<Apartment, Manager, MicroArea, ApartmentDAOObj, ManagerDAOObj, MicroAreaDAOObj>(dao, new Apartment());
        }

        [DAOTransaction]
        [RGTransaction]
        public List<Apartment> List(ApartmentsQueryParams param)
        {
            var toReturn = new List<Apartment>();
//                        DAOProvider<RGProvider>.InTransaction(
//                () =>
                   // {
            toReturn = ApartmentDAO.List(param).Select(x => FromThereToHere<Apartment, Manager, MicroArea, ApartmentDAOObj, ManagerDAOObj, MicroAreaDAOObj>(x, new Apartment())).ToList();
//                    });
            return toReturn;
        }

        [DAOTransaction]
        [RGTransaction]
        public List<int> ListPrepare(ApartmentsQueryParams param)
        {
            var toReturn = new List<int>();
//            DAOProvider<RGProvider>.InTransaction(
//                () =>
                  //  {


                        toReturn = ApartmentDAO.List(param).Select(x => x.Id).ToList();
//                    });
            return toReturn;
        }


        public static T FromThereToHere<T,TManager,TMicroArea,T2,T2Manager,T2MicroArea>(T2 source, T dest)
            where T : IApartment<TManager, TMicroArea>
            where T2 : IApartment<T2Manager, T2MicroArea>
            where TManager : class, IManager
            where TMicroArea : class, IMicroArea
            where T2Manager : class, IManager
            where T2MicroArea : class, IMicroArea
        {
            dest.Id = source.Id;
            dest.Address = source.Address;
            dest.Area = source.Area;
            dest.Cost = source.Cost;
            dest.CountOfRooms = source.CountOfRooms;
            dest.CreateDate = source.CreateDate;
            dest.CreateTime = source.CreateTime;
            dest.Description = source.Description;
            dest.Floor = source.Floor;
            dest.Manager.CopyFrom(source.Manager);
            dest.MicroArea.CopyFrom(source.MicroArea);
            dest.NumberOfStoreys = source.NumberOfStoreys;
            dest.Square = source.Square;
            dest.State = source.State;
            dest.TypeOfMaterial = source.TypeOfMaterial;
            return dest;
        }
    }
}
